﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Homework8
{
    public partial class Form1 : Form
    {
        public CreateWinForm.CreateForm createForm;
        public Orders.OrderService orderService = new Orders.OrderService();
        public ReviseForm reviseForm;
        public Form1()
        {
            InitializeComponent();
            bindingSource1.DataSource = orderService.Orders;
        }

        private void CreateBtn_Click(object sender, EventArgs e)
        {
            createForm = new CreateWinForm.CreateForm();
            createForm.Show();
            createForm.SubmitClick += SubmitOrder;
        }

        public void SubmitOrder(object sender)
        {
            orderService.Orders.Add(createForm.NewOrder);
            bindingSource1.ResetBindings(false);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            orderService.Orders.Add(createForm.NewOrder);
            bindingSource1.ResetBindings(false);
        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            orderService.Orders = orderService.Import(openFileDialog1.FileName);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            orderService.Export(folderBrowserDialog1.SelectedPath);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            reviseForm = new ReviseForm();
            reviseForm.Show();
            reviseForm.GetOrderList(orderService);
            reviseForm.ReviseClick += ReviseOrder;
            

        }
        public void ReviseOrder(Object sender)
        {
            this.orderService = reviseForm.orderService;
            bindingSource1.ResetBindings(false);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text !=  "")
            {
                orderService.DeleteOrder(Int32.Parse(textBox1.Text));
                bindingSource1.ResetBindings(false);
            }
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Orders.Order thisOrder = null;
            if (textBox2.Text != "")
                thisOrder = orderService.InquiryOrderByNum(Int32.Parse(textBox2.Text));
            if (textBox3.Text != "")
                thisOrder = orderService.InquiryOrderByClientName(textBox3.Text);
            if (comboBox1.Text != "")
                thisOrder = orderService.InquiryOrderByGoodsName(comboBox1.Text);
            richTextBox1.Text = thisOrder.ToString();
        }
    }

    
}
